package org.projects.graduates.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.projects.graduates.page.Pager;

import com.dayatang.domain.AbstractEntity;
import com.dayatang.domain.QuerySettings;

/**
 * 交作业
 * 
 * @author Administrator
 * 
 */
@Entity
@Table(name = "homework_handin")
public class HomeworkHandin extends AbstractEntity {
	private static final long serialVersionUID = -943695501349906405L;
	private final static String QL = "select count(o.id) from org.projects.graduates.domain.HomeworkHandin o ";
	@ManyToOne
	private HomeworkAssignments homework;

	@OneToOne
	private Person person;

	@Temporal(TemporalType.DATE)
	private Date time;

	// 交作业的内容
	@Lob
	private String content;

	@Enumerated(EnumType.STRING)
	@JoinColumn(name = "content_status")
	private ContentStatus status;

	private int score;

	// 打分时间
	@Temporal(TemporalType.DATE)
	private Date scoreTime;

	@OneToOne
	@JoinColumn(name = "who_score")
	private Person whoScore;

	public HomeworkHandin() {
		super();
	}

	public HomeworkHandin(HomeworkAssignments homework, Date time, ContentStatus status) {
		super();
		this.homework = homework;
		this.time = time;
		this.status = status;

	}

	public static HomeworkHandin findBy(HomeworkAssignments homework) {
		return getRepository().getSingleResult(QuerySettings.create(HomeworkHandin.class).eq("homework", homework));
	}

	public static HomeworkHandin findBy(HomeworkAssignments homework, Person currentPerson) {
		return getRepository().getSingleResult(
				QuerySettings.create(HomeworkHandin.class).eq("homework", homework).eq("person", currentPerson));
	}

	public static Pager pagerOfAll(long homeworkId, long currentPage, long pageSize) {
		return new Pager(count("where o.homework.id <> " + homeworkId + " order by o.id asc "), pageSize, currentPage);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static long count(String query) {
		String ql = QL + query;
		Map<String, Object> params = new HashMap<String, Object>();
		List<Long> count = new ArrayList(getRepository().find(ql, params));
		return null == count ? 0 : count.get(0).intValue();
	}

	public int hashCode() {
		return new HashCodeBuilder(17, 37).append(this.getId()).toHashCode();
	}

	public boolean equals(Object other) {
		if (null == other) {
			return false;
		}
		if (other instanceof HomeworkHandin) {
			HomeworkHandin o = (HomeworkHandin) other;
			if (o.getId() == this.getId()) {
				return true;
			}
		}
		return false;
	}

	@Override
	public String toString() {
		return "conent : " + this.content;
	}

	public HomeworkAssignments getHomework() {
		return homework;
	}

	public void setHomework(HomeworkAssignments homework) {
		this.homework = homework;
	}

	public Date getTime() {
		return time;
	}

	public void setTime(Date time) {
		this.time = time;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public ContentStatus getStatus() {
		return status;
	}

	public void setStatus(ContentStatus status) {
		this.status = status;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public Date getScoreTime() {
		return scoreTime;
	}

	public void setScoreTime(Date scoreTime) {
		this.scoreTime = scoreTime;
	}

	public Person getWhoScore() {
		return whoScore;
	}

	public void setWhoScore(Person whoScore) {
		this.whoScore = whoScore;
	}

}
